package com.tsystems.clientEmployee.gui;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import com.tsystems.entities.Station;

/**
 * @author vlad
 * 
 */
public class AddStationPanel extends JPanel {

	private static final long serialVersionUID = 5706735981492285638L;

	private JTextField stationNameTxt;

	private Station result;

	public AddStationPanel() {
		super(new GridBagLayout());
		setBorder(new EmptyBorder(10, 10, 5, 10));

		add(new JLabel("Наименование станции"), new GridBagConstraints(0, 0, 1,
				1, 0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 10, 10));
		stationNameTxt = new JTextField();
		stationNameTxt.setColumns(15);
		add(stationNameTxt, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		JButton okBtn = new JButton("Добавить станцию");
		JButton cancelBtn = new JButton("Отмена");
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(okBtn);
		buttonPanel.add(cancelBtn);

		add(buttonPanel, new GridBagConstraints(0, 7, 2, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		okBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Station employee = new Station(stationNameTxt.getText()
						.isEmpty() ? null : stationNameTxt.getText());
				result = employee;
				if (validate(result)) {
					AddStationPanel.this.getTopLevelAncestor()
							.setVisible(false);
				}

			}
		});

		cancelBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				result = null;
				AddStationPanel.this.getTopLevelAncestor().setVisible(false);
			}
		});
	}

	private boolean validate(Station station) {

		ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
		Validator validator = vf.getValidator();

		String message = validate(station, validator);
		if (message != null) {
			JOptionPane.showMessageDialog(AddStationPanel.this, message);
			return false;
		}

		return true;
	}

	public String validate(Object object, Validator validator) {
		Set<ConstraintViolation<Object>> constraintViolations = validator
				.validate(object);
		String resultString = null;

		if (constraintViolations.size() > 0) {
			resultString = "";

			for (ConstraintViolation<Object> cv : constraintViolations) {
				resultString += cv.getMessage() + "\n";
			}
		}
		return resultString;
	}

	private Station getResult() {
		return result;
	}

	public static Station showDialog(Window owner) {
		JDialog dialog = new JDialog(owner, "Добавление станции");
		dialog.setModal(true);
		AddStationPanel panel = new AddStationPanel();

		dialog.setContentPane(panel);
		dialog.pack();
		dialog.setLocationRelativeTo(null);
		dialog.setVisible(true);

		return panel.getResult();
	}
}
